﻿using System.Linq;
using System.Web.Mvc;
using BGCMS.Models;
using System.Collections.Generic;

namespace BGCMS.Controllers
{
    /// <summary>
    /// KeywordController class contains all logic connected to creating and modifying keywords.
    /// Only accessible to administrator users.
    /// </summary>
    [Authorize(Roles = "Administrators")]
    public class KeywordController : Controller
    {
        #region Variables

        BGRepository rep = null;

        #endregion

        #region Constructor

        public KeywordController()
        {
            rep = new BGRepository();
        }

        #endregion

        #region Private member functions

        // Returns a collection of keywords belonging to a particular MenuItem
        // Accepts an integer representing the MenuItem ID
        private IEnumerable<Keyword> GetKeywordsByMenuItem(int id)
        {
            var result = from k in rep.GetKeywords()
                         where k.MenuItemID == id
                         select k;
            return result;
        }

        #endregion

        #region General Keyword Methods

        /// <summary>
        /// Diplays a list of all keywords, grouped by their corresponding MenuItem
        /// </summary>
        /// <returns>Get: /Keyword/</returns>
        public ActionResult Index()
        {
            // Keywords belonging to "Almennt um brjóstagjöf"
            var keywords1 = GetKeywordsByMenuItem(1);

            // Keywords belonging to "Fyrstu dagarnir"
            var keywords2 = GetKeywordsByMenuItem(2);

            // Keywords belonging to "Algeng vandamál"
            var keywords3 = GetKeywordsByMenuItem(3);

            // Keywords belonging to "Næring barna"
            var keywords4 = GetKeywordsByMenuItem(4);

            // Keywords belonging to "Þroski barna"
            var keywords5 = GetKeywordsByMenuItem(5);

            // Keywords belonging to "Spurt og svarað"
            var keywords6 = GetKeywordsByMenuItem(6);

            // Keywords belonging to "Fyrir fagfólk"
            var keywords7 = GetKeywordsByMenuItem(7);

            // Keywords belonging to "Atburðir"
            var keywords8 = GetKeywordsByMenuItem(8);

            // Keywords belonging to "Ýmislegt"
            var keywords9 = GetKeywordsByMenuItem(9);

            // Keywords belonging to "Óflokkað"
            var keywords10 = GetKeywordsByMenuItem(10);

            // Keywords belonging to "Flokkað"
            var keywords11 = GetKeywordsByMenuItem(11);

            KeywordViewModel model = new KeywordViewModel
            {
                Keywords1 = keywords1,
                Keywords2 = keywords2,
                Keywords3 = keywords3,
                Keywords4 = keywords4,
                Keywords5 = keywords5,
                Keywords6 = keywords6,
                Keywords7 = keywords7,
                Keywords8 = keywords8,
                Keywords9 = keywords9,
                Keywords10 = keywords10,
                Keywords11 = keywords11
            };

            return View(model);
        }

        /// <summary>
        /// Displays a form to register a new Keyword
        /// </summary>
        /// <returns>Get: /Keyword/CreateKeyword</returns>
        public ActionResult CreateKeyword()
        {
            return View(new Keyword());
        }

        /// <summary>
        /// Register a new Keyword in the database
        /// </summary>
        /// <param name="formdata">Form fields filled out by user</param>
        /// <returns>Get: /Keyword</returns>
        [HttpPost]
        public ActionResult CreateKeyword(FormCollection formdata)
        {
            Keyword k = new Keyword();
            try
            {
                UpdateModel(k);
                rep.UpdateKeyword(k);
                rep.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {   // On failure to save to database the form is redisplayed
                return View(k);
            }
        }

        /// <summary>
        /// Displays a form to edit an existing Keyword
        /// </summary>
        /// <param name="id">The ID of the keyword</param>
        /// <returns>Get: /Keyword/EditKeyword/[id]</returns>
        public ActionResult EditKeyword(int? id)
        {
            if (id.HasValue)
            {
                var model = (from k in rep.GetKeywords()
                             where k.ID == id.Value
                             select k).SingleOrDefault();

                if (model == null)
                {
                    return View("NotFound");
                }

                return View(model);
            }
            return View("NotFound");
        }

        /// <summary>
        /// Registers changes made to an existing Keyword
        /// </summary>
        /// <param name="id">The ID of the Keyword</param>
        /// <param name="collection">Form fields filled out by user</param>
        /// <returns>Get: /Keyword</returns>
        [Authorize(Roles = "Administrators")]
        [HttpPost]
        public ActionResult EditKeyword(int id, FormCollection collection)
        {
            Keyword keyword = (from k in rep.GetKeywords()
                               where k.ID == id
                               select k).SingleOrDefault();
            try
            {
                UpdateModel(keyword);
                rep.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {   // On failure to save to database the form is redisplayed
                return View(keyword);
            }
        }

        #endregion
    }
}